﻿using System;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using TimeTravel.Components;
using TimeTravel.Extentions;
using TimeTravel.IData;

namespace TimeTravel.SqlServer  
{
    public class FriendLinkData : IFriendLinkData
    {
        #region Fields
        private static readonly SqlHelper DbHelper = new SqlHelper();
        private static readonly string[] Columns = { "[id]", "[name]", "[linkurl]", "[sort]", "[addtime]" }; 
        #endregion

        #region Implementation of IDataBase<FriendLinkInfo>

        /// <summary>
        /// 添加记录
        /// </summary>
        public int Add(FriendLinkInfo friendLink)
        {
            if (friendLink == null) return 0;
            var strSql = new StringBuilder();
            strSql.Append("insert into FriendLink (");
            strSql.Append("[name],[linkurl],[addtime],[sort]) ");
            strSql.Append(" values ");
            strSql.Append("(@name,@linkurl,@addtime,@sort); ");
            strSql.Append("select @@IDENTITY;");
            SqlParameter[] parameters = {
                                            new SqlParameter("@name",SqlDbType.NVarChar,50), 
                                            new SqlParameter("@linkurl",SqlDbType.NVarChar,100), 
                                            new SqlParameter("@addtime",SqlDbType.Int), 
                                            new SqlParameter("@sort",SqlDbType.Int)
                                        };
            parameters[0].Value = friendLink.Name;
            parameters[1].Value = friendLink.LinkUrl;
            parameters[2].Value = friendLink.AddTime;
            parameters[3].Value = friendLink.Sort;
            var result = DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters);
            return result == null ? 0 : Convert.ToInt32(result);
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            if (id == 0) return;
            var strSql = new StringBuilder();
            strSql.Append("update FriendLink set DeleteFlag = 1 ");
            strSql.Append("where [id]=@id;");
            SqlParameter[] parameters = {
                                            new SqlParameter("@id", SqlDbType.Int)
                                        };
            parameters[0].Value = id;
            DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="friendLink"></param>
        public void Update(FriendLinkInfo friendLink)
        {
            if (friendLink == null || friendLink.ID == 0) return;
            var strSql = new StringBuilder();
            strSql.Append("update FriendLink set ");
            strSql.Append("[name] = @name,");
            strSql.Append("[linkurl] = @linkurl,");
            strSql.Append("[addtime] = @addtime,");
            strSql.Append("[sort] = @sort ");
            strSql.Append("where [id] = @id ; ");
            SqlParameter[] parameters = {
                                            new SqlParameter("@name", SqlDbType.NVarChar, 50),
                                            new SqlParameter("@linkurl", SqlDbType.NVarChar, 100),
                                            new SqlParameter("@addtime", SqlDbType.Int),
                                            new SqlParameter("@sort", SqlDbType.Int),
                                            new SqlParameter("@id", SqlDbType.Int)
                                        };
            parameters[0].Value = friendLink.Name;
            parameters[1].Value = friendLink.LinkUrl;
            parameters[2].Value = friendLink.AddTime;
            parameters[3].Value = friendLink.Sort;
            parameters[4].Value = friendLink.ID;
            DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public IList<FriendLinkInfo> GetAll()
        {
            var strSql = new StringBuilder();
            strSql.AppendFormat("select {0} from FriendLink ", String.Join(",", Columns));
            strSql.Append("where DeleteFlag = 0;");
            var result = DbHelper.ExecuteObjectList(strSql.ToString(), DataReaderParser);
            return result.ToList();
        }

        /// <summary>
        /// 根据条件获取
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public RecordSet<FriendLinkInfo> GetBySearch(PageQueryInfo query)
        {
            if(query == null) return new RecordSet<FriendLinkInfo>();
            return DbHelper.ExecutePaging("FriendLink", query, DataReaderParser, Columns);
        }

        /// <summary>
        /// 获取详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FriendLinkInfo GetOne(int id)
        {
            if (id == 0) return null;
            var strSql = new StringBuilder();
            strSql.AppendFormat("select {0} from FriendLink ", String.Join(",", Columns));
            strSql.Append("where [id] = @id;");
            SqlParameter[] parameters = {
                                            new SqlParameter("@id", SqlDbType.Int)
                                        };
            parameters[0].Value = id;
            return DbHelper.ExecuteObject(CommandType.Text, strSql.ToString(), DataReaderParser, parameters);
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <returns></returns>
        public int GetTotalCount()
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(id) from FriendLink ");
            strSql.Append("where DeleteFlag = 0;");
            var result = DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString());
            return result == null ? 0 : Convert.ToInt32(result);
        }

        #endregion

        #region BuildNavMenuObject

        /// <summary>
        /// 从DataReader中构建对象
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private FriendLinkInfo DataReaderParser(IDataReader reader)
        {
            var friendLink = new FriendLinkInfo
                                 {
                                     ID = reader.GetInt32("id"),
                                     Name = reader.GetString("name"),
                                     LinkUrl = reader.GetString("linkurl"),
                                     Sort = reader.GetInt32("sort")
                                 };
            return friendLink;
        }

        #endregion
    }
}

